package cn.zyq.ykb.service.task.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.task.HomeChartMapper;
import cn.zyq.ykb.beans.task.HomeChart;
import cn.zyq.ykb.service.task.HomeChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 首页-轮播图Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-06-01
 */
@Service
public class HomeChartServiceImpl  extends BaseService implements HomeChartService {

    @Autowired
    private HomeChartMapper homeChartMapper;

    /**
     * 查询首页-轮播图
     *
     * @param homeChartID 首页-轮播图主键
     * @return 首页-轮播图
     */
    @Override
    public HomeChart selectHomeChartByHomeChartID(Integer homeChartID) {
        HomeChart homeChart = homeChartMapper.selectByPrimaryKey(homeChartID);
        if (homeChart.getDeleted() == 1) {
            return null;
        }
        return homeChart;
    }

    /**
     * 查询首页-轮播图
     *
     * @param serviceID 业务ID
     * @return 首页-轮播图
     */
    @Override
    public HomeChart selectHomeChartByServiceID(String serviceID) {
        HomeChart param = new HomeChart();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.homeChartMapper.selectOne(param);
    }

    /**
     * 查询首页-轮播图列表
     *
     * @param pageParam
     * @return 首页-轮播图
     */
    @Override
    public PageInfo<HomeChart> selectHomeChartList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<HomeChart> list = this.homeChartMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询首页-轮播图列表
     *
     * @param param
     * @return 首页-轮播图
     */
    @Override
    public List<HomeChart> selectHomeChartList(HomeChart param) {
        param.setDeleted(0);
        return this.homeChartMapper.select(param);
    }
    /**
     * 新增首页-轮播图
     *
     * @param param 首页-轮播图
     * @return 结果
     */
    @Override
    public int insertHomeChart(HomeChart param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.homeChartMapper.insertSelective(param);
    }

    /**
     * 修改首页-轮播图
     *
     * @param param 首页-轮播图
     * @return 结果
     */
    @Override
    public int updateHomeChart(HomeChart param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.homeChartMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除首页-轮播图
     *
     * @param homeChartIDs 需要删除的首页-轮播图主键
     * @return 结果
     */
    @Override
    public int deleteHomeChartByHomeChartIDs(Integer[] homeChartIDs) {
        int i = 0;
        for (Integer homeChartID : homeChartIDs) {
            i += this.deleteHomeChartByHomeChartID(homeChartID);
        }
        return i;
    }

    /**
     * 删除首页-轮播图信息
     *
     * @param homeChartID 首页-轮播图主键
     * @return 结果
     */
    @Override
    public int deleteHomeChartByHomeChartID(Integer homeChartID) {
        HomeChart homeChart = this.selectHomeChartByHomeChartID(homeChartID);
        if (homeChart == null) {
            return 0;
        } else {
            HomeChart storeInfo = new HomeChart();
            storeInfo.setHomeChartID(homeChartID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.homeChartMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
